package ru.rchervot.sanasar.client.view;

import ru.rchervot.sanasar.client.model.SearchResultsItem;
import ru.rchervot.sanasar.client.model.SearchResultsItemCandidates;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.inject.Inject;


public class ResultsCandidatesView extends AbstractResultsView 
implements ResultsCandidatesPresenter.Display, ClickHandler {

	private class Col1Widget extends VerticalPanel {
		private final HTML lineName = new HTML("", false);
		private final HTML lineLang = new HTML("", false);
		private final HTML lineEmpl = new HTML("", false);
		private final HTML lineOrder = new HTML("", false);
//		private final HTML lineAction = new HTML("", false);
		private final HTML lineNote = new HTML("", false);
		
		public Col1Widget() {
			this.setStylePrimaryName("ResultsCandidatesView_Item");
			
			this.add(lineName);
			this.add(lineLang);
			this.add(lineEmpl);
			this.add(lineOrder);
//			this.add(lineAction);
			this.add(lineNote);
		}

		public HTML getLineName() {
			return lineName;
		}

		public HTML getLineLang() {
			return lineLang;
		}

		public HTML getLineEmpl() {
			return lineEmpl;
		}

		public HTML getLineOrder() {
			return lineOrder;
		}

//		public HTML getLineAction() {
//			return lineAction;
//		}

		public HTML getLineNote() {
			return lineNote;
		}
		
	}
	
	private class Col2Widget extends HTML {
		public Col2Widget() {
			super();
			
			this.setWordWrap(false);
		}
		
		@Override
		public void setHTML(String html) {
			super.setHTML(html);
		}
	}
	
	private final static int COLUMNS_COUNT = 3; 
	
	@Inject
	public ResultsCandidatesView() {
		super();
	}
	
	@Override
	protected Col1Widget createCol1Widget() {
		return new Col1Widget();
	}
	
	@Override
	protected Col2Widget createCol2Widget() {
		return new Col2Widget();
	}
	
	@Override
	protected void updateCol2Widget(int row, SearchResultsItem i) {
		SearchResultsItemCandidates item = (SearchResultsItemCandidates)i;
		
		StringBuffer sb = new StringBuffer();
		
		if (item.getCompensationView() != null) {
			sb.append("<span class=\"ResItemCandCompensation\">" + item.getCompensationView() + "</span>" + "&nbsp;");
		}
		
		if (item.getAgeView() != null) {
			sb.append("<span class=\"ResItemCandAge\">" + item.getAgeView() + "</span>" + "&nbsp;");
		}
		
		if (item.getViewed() != null) {
			if (sb.length() > 0) {
				sb.append("<br/>");
			}
			sb.append("<span class=\"ResItemViewed\">" + 
					DateTimeFormat.getShortDateFormat().format(item.getViewed()) + "</span>");
		}
		
		((Col2Widget)widgetsCol2.get(row)).setHTML(sb.toString());
	}
	
	@Override
	protected void updateCol1Widget(int row, SearchResultsItem i) {
		SearchResultsItemCandidates item = (SearchResultsItemCandidates)i;
		Col1Widget w = (Col1Widget)widgetsCol1.get(row);
		
		w.getLineName().setHTML(
				"<span class=\"ResItemCandName\">" + 
				(item.getCandName() == null ? "-No Name-" : item.getCandName()) + 
				"</span>" + "&nbsp;" +
				"<span class=\"ResItemCandRegion\">" + 
				(item.getRegionName() == null ? "" : item.getRegionName()) + 
				"</span>" + "&nbsp;" +
				"<span class=\"ResItemCandId\">" + item.getItemID() + "</span>");
		

		if (item.getLanguages() != null) {
			w.getLineLang().setHTML(
					"<span class=\"ResItemCandLang\">" + item.getLanguages() + "</span>");	
			w.getLineLang().setVisible(true);
		} else {
			w.getLineLang().setVisible(false);
			w.getLineLang().setHTML("");
		}
		
		String emplLine = composeEmplLine(item);
		if (emplLine != null && emplLine.length() > 0) {
			w.getLineEmpl().setHTML(emplLine);
			w.getLineEmpl().setVisible(true);
		} else {
			w.getLineEmpl().setVisible(false);
			w.getLineEmpl().setHTML("");
		}
		
		String orderLine = composeOrderLine(item);
		if (orderLine != null && orderLine.length() > 0) {
			w.getLineOrder().setHTML(orderLine);
			w.getLineOrder().setVisible(true);
		} else {
			w.getLineOrder().setVisible(false);
			w.getLineOrder().setHTML("");
		}
		
//		if (item.getContactDatetime() != null) {
//			w.getLineAction().setHTML(
//					DateTimeFormat.getShortDateFormat().format(item.getContactDatetime()) + "&nbsp;" +
//					item.getContactResult() +"&nbsp;" +
//					item.getContactCreatorName());	
//			w.getLineAction().setVisible(true);
//		} else {
//			w.getLineAction().setVisible(false);
//			w.getLineAction().setHTML("");
//		}
		
		if (item.getContactNote() != null) {
			w.getLineNote().setHTML(
					"<span class=\"ResItemContNote\">" + item.getContactNote() + "</span>");	
			w.getLineNote().setVisible(true);
		} else {
			w.getLineNote().setVisible(false);
			w.getLineNote().setHTML("");
		}
	}

	private static final int COMPANY_NAME_LENGTH = 20;
	
	private String composeEmplLine(SearchResultsItemCandidates item) {
		StringBuffer sb = new StringBuffer();
		
		if (item.getEmplCompany() != null) {
			sb.append("<span class=\"ResItemEmplComp\">" + 
					resizeString(item.getEmplCompany(), COMPANY_NAME_LENGTH) + 
					"</span>" + "&nbsp;");	
		}
		
		if (item.getEmplTitle() != null) {
			sb.append("<span class=\"ResItemEmplTitl\">" + item.getEmplTitle() + "</span>");	
		}
		
		return sb.toString();
	}
	
	private String composeOrderLine(SearchResultsItemCandidates item) {
		StringBuffer sb = new StringBuffer();
		
		if (item.getContactCompany() != null) {
			sb.append("<span class=\"ResItemContComp\">" + 
					resizeString(item.getContactCompany(), COMPANY_NAME_LENGTH) + 
					"</span>" + "&nbsp;");	
		}
		
		if (item.getContactPositionTitle() != null) {
			sb.append("<span class=\"ResItemPosTitl\">" + item.getContactPositionTitle() + 
					"</span>" + "&nbsp;");
		}
		
		if (item.getContactComp() != null) {
			sb.append("<span class=\"ResItemContMoney\">" + item.getContactComp() + "</span>");	
		}
		
		return sb.toString();
	}
	
	public static String resizeString(String s, int newLenght) {
    if (s == null) return s;
    
    String d = "...";
    int l = s.length();
    
    if (l < (d.length() + 2)) return s;
    
    int m = (newLenght - d.length()) / 2;
    
    if (s.length() > newLenght) {
        return s.substring(0, m) + "..." +  s.substring(l - m);
    } else {
        return s;
    }
}
	
	@Override
	protected int getColumnsCount() {
		return COLUMNS_COUNT;
	}


}
